package com.thingsgrid.appmanager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thingsgrid.appmanager.entity.ApplicationInfo;
import com.thingsgrid.appmanager.entity.LocalDevice;
import com.thingsgrid.appmanager.mapper.IApplicationsMapper;
import com.thingsgrid.appmanager.service.IApplicationsService;
import com.thingsgrid.device.entity.Device;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class IApplicationsServiceImpl extends ServiceImpl<IApplicationsMapper, ApplicationInfo> implements IApplicationsService {

	private IApplicationsMapper mapper;

	@Override
	public List<ApplicationInfo> showApp() {
		return mapper.showApp();
	}

	@Override
	public List<ApplicationInfo> getApplications(Device device) {
		List<ApplicationInfo> list = new ArrayList<>();
		String device_id = device.getDeviceId();
		list = mapper.getApplications(device_id);
		return list;

	}

	@Override
	public Float getNewVersion(String id) {
		return mapper.getNewVersion(id);
	}

	@Override
	public Page<ApplicationInfo> selectAppPage(Page<ApplicationInfo> page, String device_id, Float version, String application_name) {
		page.setOptimizeCountSql(false);
		page.setSearchCount(true);
		if (application_name == null || application_name.equals("")) {
			return page.setRecords(mapper.getAppByDevice_1(page, device_id, version));
		}else {
			return page.setRecords(mapper.getAppByDevice_2(page, device_id, version, application_name));
		}


	}

	@Override
	public List<ApplicationInfo> getApp(String app_name, Float version, String device_id) {
		List<ApplicationInfo> list = mapper.getApp(app_name, version, device_id);
		return list;
	}

	@Override
	public Page getApppage(Page page, String app_name, Float version, String device_id) {
		Page iPage = new Page();
		iPage = mapper.getApppage(page, app_name, version, device_id);
		return iPage;
	}

	@Override
	public List<LocalDevice> getdevice(long size, long current) {
		//List<LocalDevice> list = this.restTemplate.getForObject("http://localhost:8000/hosue/" + id, LocalDevice.class);
		return null;
	}


}
